<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/20
 * Time: 13:31
 */

namespace app\modules\setting\controllers\auth;


use app\common\controllers\AdminOptionsController;
use app\common\enum\Adminuser_typeEnum;
use app\common\enum\EtcSellerTiktokProduct_shStatusEnum;
use app\common\helper\CommonHelper;
use app\models\EtcSellerTiktokProduct;
use app\models\EtcSellerTiktokProductEdit;
use app\models\QlUserTiktokProductGoods;
use app\modules\setting\configs\auth\ShsjcpConfig;
use app\modules\setting\models\cpgl\QlUserTiktokProductGoodsSearch;
use app\modules\setting\models\seller\EtcSellerTiktokProductSearch;
use sookie\auth\models\Adminuser;
use sookie\db\DBHandler;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use Yii;

class ShsjcpController extends AdminOptionsController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcSellerTiktokProductSearch();
            $dataProvider = $searchModel->searchSellerSh(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $re = $item;
                    if(is_object($item)){
                        $re = $item->toArray();
                    }
                    $re['name'] = CommonHelper::getProductName($item);
                    $re['seller_commission_percent'] = CommonHelper::float($item['seller_commission_percent'])."%";
                    $re['creator_commission_percent'] = CommonHelper::float($item['creator_commission_percent'])."%";
                    return $re;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],ShsjcpConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
     * @param $id
     * @return array
     * 修改接口
     */
    public function actionSubmit($id)
    {
        $tran = Yii::$app->db->beginTransaction();
        try {

            $model = $this->findModel($id);

            if (Yii::$app->request->isPost){
                $_POST['is_apply'] = 0;
                $link = Yii::$app->request->post("link");
                if(empty($link)){
                    throw new Exception("请填写分佣链接");
                }
                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcSellerTiktokProduct::tableName(),Yii::$app->request->post()) && $model->save()) {
                    if($model->sh_status == EtcSellerTiktokProduct_shStatusEnum::REFUSE && empty($model->refuse_mark)){
                        throw new Exception("请填写驳回说明");
                    }
                    $tran->commit();
                    return ["msg" => "审核成功"];
                }
                throw new Exception(CommonHelper::getFirstErrorStr($model->errors));
            }
            return ShsjcpConfig::configSubmit($model);
        }catch (\Throwable $e){
            $tran->rollBack();
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    protected function findModel($id)
    {
        if (($model = EtcSellerTiktokProduct::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }
}